#ifndef GATEWAY_CONNECTION_H
#define GATEWAY_CONNECTION_H

#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <string>
#include <unordered_map>
#include <mutex>

namespace chat {
using server_t = websocketpp::server<websocketpp::config::asio>;

struct Client {
    std::string user_id;    // 用户ID
    std::string session_id; // 登录会话ID

    Client(const std::string& uid, const std::string& ssid)
        : user_id(uid), session_id(ssid) {}
};
using ClientPtr = std::shared_ptr<Client>;

class Connection {
public:
    bool insert(const server_t::connection_ptr& conn, const ClientPtr client);
    server_t::connection_ptr connection(const std::string& user_id);
    ClientPtr client(const server_t::connection_ptr& conn);
    bool remove(const server_t::connection_ptr& conn);

private:
    std::mutex mutex_;
    std::unordered_map<std::string, server_t::connection_ptr> uid_conn_map;
    std::unordered_map<server_t::connection_ptr, ClientPtr> conn_client_map;
};
}  // namespace chat


#endif // GATEWAY_CONNECTION_H